package com.jianyun.jyzs.widget.badge;


import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.util.Log;


public class BadgeHandler implements IBadgeHandler {
    private static final String TAG = BadgeHandler.class.getSimpleName();

    public static final String TOTAL_UNREAD_COUNT = "total_unread_count";
    public static final String UNREAD_COUNT = "unread_count";

    private static IBadgeHandler badgeHandler;
    private static SharedPreferences sp;
    private static SharedPreferences.Editor editor;

    private BadgeHandler() {
    }

    private static class SingletonHolder {
        static BadgeHandler INSTANCE = new BadgeHandler();
    }

    public static BadgeHandler getInstance() {
        return SingletonHolder.INSTANCE;
    }


    public static void init(Context context) {
        sp = context.getSharedPreferences(TOTAL_UNREAD_COUNT, Context.MODE_PRIVATE);
        editor = sp.edit();
        Log.d(TAG, "Build.MANUFACTURER: " + Build.MANUFACTURER);
        if (Build.MANUFACTURER.equalsIgnoreCase("HUAWEI")) {
            // luncherPackageName : com.huawei.android.internal.app
            badgeHandler = new HuaweiBadgeHandler(context);
            return;
        }
        if (Build.MANUFACTURER.equalsIgnoreCase("vivo")) {
            badgeHandler = new VivoBadgeHandler(context);
            return;
        }
        if (Build.MANUFACTURER.equalsIgnoreCase("samsung")) {
            badgeHandler = new SamsungBadgeHandler(context);
            return;
        }
        if (Build.MANUFACTURER.equalsIgnoreCase("HTC")) {
            badgeHandler = new HtcBadgeHandler(context);
            return;
        }
    }

    @Override
    public void updateBadgeCount(int count) {
        if (badgeHandler != null) {
            Log.d(TAG, "updateBadgeCount " + count);
            badgeHandler.updateBadgeCount(count);
        }
        if (editor != null) {
            editor.putInt(UNREAD_COUNT, count);
            editor.apply();
        }
    }

    public void updatePushMessageCount(){
        if (editor != null) {
            int count = sp.getInt(UNREAD_COUNT, 0) + 1;
            editor.putInt(UNREAD_COUNT, count);
            editor.apply();
            updateBadgeCount(count);
        }
    }
}
